package com.caojing.haolemai.ui.product;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.caojing.haolemai.MyApp;
import com.caojing.haolemai.R;
import com.caojing.haolemai.base.BaseActivity;
import com.caojing.haolemai.bean.ProductBean;
import com.caojing.haolemai.db.CartBean;
import com.caojing.haolemai.db.CollectBean;
import com.caojing.haolemai.ui.cart.CartActivity;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;

import butterknife.Bind;
import butterknife.OnClick;

public class ProductActivity extends BaseActivity implements ProductContract.View {

    @Bind(R.id.product_detail_ivBack)
    ImageView productDetailIvBack;
    @Bind(R.id.product_detail_tvTitle)
    TextView productDetailTvTitle;
    @Bind(R.id.product_detail_iv)
    ImageView productDetailIv;
    @Bind(R.id.product_detail_tvPrice)
    TextView productDetailTvPrice;
    @Bind(R.id.product_detail_tvName)
    TextView productDetailTvName;
    @Bind(R.id.product_detail_tvCount)
    TextView productDetailTvCount;
    @Bind(R.id.product_detail_tvCar)
    ImageView productDetailTvCar;
    @Bind(R.id.product_detail_RRCar)
    RelativeLayout productDetailRRCar;
    @Bind(R.id.product_detail_ivCollect)
    ImageView productDetailIvCollect;
    @Bind(R.id.product_detail_tvCollect)
    TextView productDetailTvCollect;
    @Bind(R.id.product_detail_llCollect)
    LinearLayout productDetailLlCollect;
    @Bind(R.id.product_detail_tvAdd)
    TextView productDetailTvAdd;
    @Bind(R.id.product_detail_tvBuy)
    TextView productDetailTvBuy;
    private ProductContract.Presenter presenter;
    private int index =0;
    private ProductBean product;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_product;
    }

    @Override
    protected void initView() {
        presenter = new ProductPresenter(this);
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        presenter.initData(id);
    }

    @Override
    public void onSuccessGetProduct(ProductBean productBean) {
        productDetailTvTitle.setText(productBean.getResults().getBrandName());
        product = productBean;
        Glide.with(this).load(productBean.getResults().getPhotos().getL1())
                .centerCrop()
                .placeholder(R.mipmap.global_default_image_outlets)
                .error(R.mipmap.global_default_image_outlets).into(productDetailIv);
        productDetailTvPrice.setText("￥"+productBean.getResults().getSalePrice());
        productDetailTvName.setText(productBean.getResults().getName());
    }

    @Override
    public void onFailGetProduct(String msg) {
        Toast.makeText(ProductActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @OnClick({R.id.product_detail_ivBack, R.id.product_detail_RRCar, R.id.product_detail_llCollect, R.id.product_detail_tvAdd, R.id.product_detail_tvBuy})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.product_detail_ivBack:
                finish();
                break;
            case R.id.product_detail_RRCar:
                startActivity(new Intent(this, CartActivity.class));
                break;
            case R.id.product_detail_llCollect:
                if (index%2==0){
                    productDetailIvCollect.setImageResource(R.mipmap.collect);
                    productDetailTvCollect.setText("已收藏");
                    if (product!=null){
                        String name = product.getResults().getName();
                        String price = product.getResults().getSalePrice()+"";
                        String url = product.getResults().getPhotos().getL1();
                        String pid = product.getResults().getProductId()+"";
                        CollectBean collectBean = new CollectBean(name,price,url,pid);
                        try {
                            MyApp.dbUtils.save(collectBean);
                        } catch (DbException e) {
                            e.printStackTrace();
                        }
                    }
                    Toast.makeText(ProductActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                }else{
                    productDetailIvCollect.setImageResource(R.mipmap.un_collect);
                    productDetailTvCollect.setText("收藏");
                    Toast.makeText(ProductActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                    try {
                        MyApp.dbUtils.delete(CollectBean.class, WhereBuilder.b("pid", "=",product.getResults().getProductId()+""));
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                }
                index++;
                break;
            case R.id.product_detail_tvAdd:
                Toast.makeText(ProductActivity.this, "添加购物车成功", Toast.LENGTH_SHORT).show();
                if (product!=null){
                    String name = product.getResults().getName();
                    String price = product.getResults().getSalePrice()+"";
                    String url = product.getResults().getPhotos().getL1();
                    String pid = product.getResults().getProductId()+"";
                    CartBean cartBean = new CartBean(name,price,url,pid);
                    try {
                        MyApp.dbUtils2.save(cartBean);
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.product_detail_tvBuy:
                Toast.makeText(ProductActivity.this, "支付宝未安装", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
